package com.sskj.jgxt.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sskj.jgxt.jd.vo.ListInfoVO;
import com.sskj.jgxt.jd.vo.ResultMegVO;
import com.sskj.jgxt.repository.JianDuEngRepository;
import com.sskj.personnel.repository.EmployeeRepository;
import com.sskj.sys.domain.entity.MediaEntity;
import com.sskj.sys.repository.MediaRepository;
import com.sskj.utils.StringUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/wechat", method = { RequestMethod.POST, RequestMethod.GET })
@Transactional
@Api(value = "附件记录", description = "附件记录")
public class WeChartImgServiceImpl {

	protected final Log logger = LogFactory.getLog(WeChartImgServiceImpl.class);



	@Autowired
	JianDuEngRepository engrepository;

	@Autowired
	EmployeeRepository employeeRepository;

	@Value("${sskj.conf.restUrl}")
	private String restUrl;
	
	@Autowired
	MediaRepository mediaRepository;
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/img/getFiles", method = { RequestMethod.POST })
	@ApiImplicitParams({ @ApiImplicitParam(name = "spId", value = "记录ID", paramType = "form", dataType = "string") })
	@ApiOperation(value = "获取附件记录", notes = "获取附件记录", httpMethod = "GET")
	public List<MediaEntity> getMediaEntity(String spId) throws Exception {
		
		List<MediaEntity> page = mediaRepository.queryPic(spId);
		return page;
	}
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/img/saveFiles", method = { RequestMethod.POST })	
	@ApiOperation(value = "新增附件记录", notes = "新增附件记录", httpMethod = "POST")
	public ResultMegVO savefile(@RequestBody ListInfoVO jl) throws Exception {
		ResultMegVO result = new ResultMegVO();
		if(StringUtils.isNotEmpty(jl.getGlid()))
		{
			List<String> idsList=jl.getIds();		
		
			if (!idsList.isEmpty()) {
				mediaRepository.updateMedia(jl.getGlid(),idsList);
			}

			mediaRepository.delMedia(jl.getGlid(), idsList.isEmpty()
					? StringUtil.toList("XXXXX", ",") : idsList);
		
			logger.info("输出" + result.getCode());
		}
		return result;
		
	}
	



}
